<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Mail extends CH_Controller{

	/**
	 * Index Page for this controller.
	 *
	 * Maps to the following URL
	 * 		http://example.com/index.php/welcome
	 *	- or -  
	 * 		http://example.com/index.php/welcome/index
	 *	- or -
	 * Since this controller is set as the default controller in 
	 * config/routes.php, it's displayed at http://example.com/
	 *
	 * So any other public methods not prefixed with an underscore will
	 * map to /index.php/welcome/<method_name>
	 * @see http://codeigniter.com/user_guide/general/urls.html
	 */
	 /**
         * Constructor
         */
	function __construct() { 
		parent::__construct(); 
		$this->init();
		$this->load->model(array('mail_model','setting_model'));
		$this->load->helper('file');
    } 
    	
	
	/**
	 * 메일전송 폼
	 *
	 * @access	public
	 * @return	array
	 */
	function mailForm(){
		login_admin();
		$this->load->helper('directory');
		/*$template = null;
		if($this->input->post('template')){
			$template = read_file('./template/mail/'.$this->input->post('template').'/mail.html');
			$template  = html_content($template);
		}*/
		$url = $this->uri_church();
		$dic = directory_map('./template/mail',1);
		$target = $this->mail_model->select_count_target();
		
		
		$data = array(
				'_title' => '메일관리',
				'_group' => 'member',
				'_content' => $this->config->item('ch_manager_root').'/'.$url,
				'template_list' => $dic,
				'target' => $target
			);
		return $data ;
	}
	
	/**
	 * 메일 미리보기
	 *
	 * @access	public
	 * @return	array
	 */
	function mailPreview(){
		$this->load->helper('text');
		$url = $this->uri_church();
		
		$content = $this->mail_model->get_mail_body();
		$data = array(
				'_content' => $this->config->item('ch_manager_root').'/'.$url,
				'_use_theme' => 'N',
				'content' => $content
			);
		return $data;
	}
	
	/**
	 * 메일 전송
	 *
	 * @access	public
	 * @return	void
	 */
	function mailSend(){
		$this->load->library('email');
		$target = $this->input->post('target');
		$title = $this->input->post('title');
		$mail_id = $this->input->post('mail_id');
		$mail_domain = $this->input->post('mail_domain');
		$email = "{$mail_id}@{$mail_domain}";
		$content = $this->mail_model->get_mail_body();
		
		switch($target){
			case('total'):
				$where = array('mailling_yn'=>'Y');
			break;
			case('birthday'):
				$where = array('mailling_yn'=>'Y','SUBSTRING(birth_date,6,5)'=>date('m-d'));
			break;
		}
		
		$array_to = null;
		if(is_array($where)){
			foreach($this->mail_model->select_target($where) as $row){
				$array_to[] = array('name'=>$row->name,'email'=>$row->email);
			}
		}else{
			$array_to[] = array('name'=>'','email'=>$email);
		}
		
		$site_info = $this->setting_model->select_siteinfo();
		$site_email = $site_info['mail_id'].'@'.$site_info['mail_domain'];
		for($i=0; $i < sizeof($array_to); $i++){
			
			$content = $this->mail_model->get_mail_body();
			$this->email->from($site_email, '교회명');
			$this->email->to($array_to[$i]['email'],$array_to[$i]['name']);
			$this->email->subject($title);
			$this->email->message($content);			
			if ( ! $this->email->send()){
				echo '에러'; //에러 로그 구현
			}
			
			if($i > 0 && (($i % 1000) === 0) ){
				sleep(1000);
			}
		}
		
		//echo $this->email->print_debugger();
		$this->alert->jalert('메일 발송 완료', base_url('/mail/mailForm'));
		
		
		
		
		
		//exit;
	}
}

/* End of file html.php */
/* Location: ./application/controllers/mail.php */